/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLEUR;

import CONTROLEUR.exceptions.NonexistentEntityException;
import CONTROLEUR.exceptions.PreexistingEntityException;
import MODEL.Commander;
import MODEL.CommanderPK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MODEL.Fournisseur;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author ahmed
 */
public class CommanderJpaController implements Serializable {

    public CommanderJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Commander commander) throws PreexistingEntityException, Exception {
        if (commander.getCommanderPK() == null) {
            commander.setCommanderPK(new CommanderPK());
        }
        commander.getCommanderPK().setIdF(commander.getFournisseur().getIdF());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Fournisseur fournisseur = commander.getFournisseur();
            if (fournisseur != null) {
                fournisseur = em.getReference(fournisseur.getClass(), fournisseur.getIdF());
                commander.setFournisseur(fournisseur);
            }
            em.persist(commander);
            if (fournisseur != null) {
                fournisseur.getCommanderCollection().add(commander);
                fournisseur = em.merge(fournisseur);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCommander(commander.getCommanderPK()) != null) {
                throw new PreexistingEntityException("Commander " + commander + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Commander commander) throws NonexistentEntityException, Exception {
        commander.getCommanderPK().setIdF(commander.getFournisseur().getIdF());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Commander persistentCommander = em.find(Commander.class, commander.getCommanderPK());
            Fournisseur fournisseurOld = persistentCommander.getFournisseur();
            Fournisseur fournisseurNew = commander.getFournisseur();
            if (fournisseurNew != null) {
                fournisseurNew = em.getReference(fournisseurNew.getClass(), fournisseurNew.getIdF());
                commander.setFournisseur(fournisseurNew);
            }
            commander = em.merge(commander);
            if (fournisseurOld != null && !fournisseurOld.equals(fournisseurNew)) {
                fournisseurOld.getCommanderCollection().remove(commander);
                fournisseurOld = em.merge(fournisseurOld);
            }
            if (fournisseurNew != null && !fournisseurNew.equals(fournisseurOld)) {
                fournisseurNew.getCommanderCollection().add(commander);
                fournisseurNew = em.merge(fournisseurNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                CommanderPK id = commander.getCommanderPK();
                if (findCommander(id) == null) {
                    throw new NonexistentEntityException("The commander with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(CommanderPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Commander commander;
            try {
                commander = em.getReference(Commander.class, id);
                commander.getCommanderPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The commander with id " + id + " no longer exists.", enfe);
            }
            Fournisseur fournisseur = commander.getFournisseur();
            if (fournisseur != null) {
                fournisseur.getCommanderCollection().remove(commander);
                fournisseur = em.merge(fournisseur);
            }
            em.remove(commander);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Commander> findCommanderEntities() {
        return findCommanderEntities(true, -1, -1);
    }

    public List<Commander> findCommanderEntities(int maxResults, int firstResult) {
        return findCommanderEntities(false, maxResults, firstResult);
    }

    private List<Commander> findCommanderEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Commander.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Commander findCommander(CommanderPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Commander.class, id);
        } finally {
            em.close();
        }
    }

    public int getCommanderCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Commander> rt = cq.from(Commander.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
